package com.fourth.shopping.controller;



import com.fourth.shopping.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping("/")
    public String wellcomePage() {
        return "login.html";
    }


    @RequestMapping("/loginData.do")
    @ResponseBody
    public User getLoginDate(HttpSession session) {

        String username = (String) session.getAttribute("userDate");
        User user = new User();
        user.setUName(username);
        return user;
    }

    @PostMapping("/login.do")
    public String LoginAdmin(String username, String password, HttpSession session, HttpServletResponse response) {
        System.out.println("================");
//
//        Subject subject = SecurityUtils.getSubject();
//
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//
//        Cookie cookie = new Cookie("username",username);
//
//        response.addCookie(cookie);
//        try {
//            subject.login(token);
//            session.setAttribute("userDate", username);
//            return "redirect:index.html";
//        } catch (UnknownAccountException ex) {
//            System.out.println("message用户名没有找到");
//            return "redirect:login.html";
//        } catch (IncorrectCredentialsException ex) {
//            System.out.println("message用户名密码不匹配");
//            return "redirect:login.html";
//        } catch (AuthenticationException e) {
//            System.out.println("message其他的登录错误");
//            return "redirect:login.html";
//        }
        return "index.html";

    }

    @RequestMapping("/tologin.do")
    public String toLogin() {
        return "login.html";
    }
}
